<!--#include file="../nav/admin_top.php" -->
<style type="text/css">
.sourcetb_out{
width:950px;
border:0px solid #CC3300;
overflow-x:scroll;
}
table.sourcetb{
border-collapse:collapse;
}
table.sourcetb th{
width:150px;
border: 1px solid #CCCCCC;
background-color: #E2F4C4;
padding: 5px;
text-align: center;
vertical-align: top;
}
table.sourcetb td{
border: 1px solid #CCCCCC;
background-color: #fcfcfc;
padding: 5px;
text-align: center;
vertical-align: top;
}
</style>
<div class="titleDiv">
	<div class="titleCaption">
		<span class="captionSpan">溯源管理 - 配送溯源录入</span>
	</div>
</div>
<div class="blockdiv">
	<div class="titlediv">配送批次信息</div>
	<table class="mytable" > 
		<tr>	
			<td class="mylabel"> 配送批次编号：</td>
			<td class="myinfo" style="color:#333333;font-size:14px;" >
				{$rs_source.code}
			</td>
			<td class="mylabel">配送日期：</td>
			<td class="myinfo" style="color:#FF3333;font-size:14px;" > 
				{$rs_source.senddate}
			</td>
			<td class="mylabel">溯源录入状态：</td>
			<td class="myinfo" style="color:#FF3333;font-size:14px;" > 
				<?PHP
				if($rs_source['pro_flag'] == '0'){
					echo '<div class="noflag">供应溯源</div>';
				}else{
					echo '<div class="yesflag">供应溯源</div>';
				}
				if($rs_source['make_flag'] == '0'){
					echo '<div class="noflag">生产溯源</div>';
				}else{
					echo '<div class="yesflag">生产溯源</div>';
				}
				if($rs_source['send_flag'] == '0'){
					echo '<div class="noflag">配送溯源</div>';
				}else{
					echo '<div class="yesflag">配送溯源</div>';
				}
				?>
			</td>			
		</tr>  
	</table> 
</div>
<div class="sourcetb_out" >
	<div style="width:<?PHP echo ( 3 + 2 + count($send_fields) )*162+20; ?>px">
		<table class="sourcetb" id="sourcetb">
			<tr>
				<th>小区名称</th>
				<th>会员编号</th>
				<th>会员名</th>
				 
				<th>
					出库时间
					<br><button class="autobtn" onclick="fnAllAuto('send_outtime')">自动</button>
					<button class="autobtn" onclick="fnAllInputChu()">批量</button>
				</th>	
				<th>
					配送人员
					<br><button class="autobtn" onclick="fnAllAuto('send_man')">自动</button>
					<button class="autobtn" onclick="fnAllInput('send_man','配送人员')">批量</button>
				</th>	
				<?PHP
				foreach($send_fields as $k=>$v){
					if(!is_array($v['option'])){
						$v['option'] = array();
					}
					$options = implode(',',$v['option']);
					echo '<th>'.$v['field_name'].'<br><button class="autobtn" onclick="fnAllInputField(\''.$v['id'].'\',\''.$v['field_name'].'\',\''.$v['field_type'].'\',\''.$v['field_length'].'\',\''.$v['field_decimal_len'].'\',\''.$options.'\')">批量</button></th>';
				}
				?>
			</tr>
			<?PHP
			$rs_xiaoqu = array();
			foreach($rs_source_user as $k=>$val){
				$rs_xiaoqu[] = $val['zone'];
				echo '<tr id="tr_'.$k.'" index="'.$k.'" class="tr_source" myid="'.$val['id'].'" >';
				echo '	<td >'.$val['zone'].'</td>';
				echo '	<td >'.$val['username'].'</td>';
				echo '	<td >'.$val['name'].'</td>';
				echo '	<td >';
				echo '		<input type="text" class="textBox send_outtime" id="send_outtime_'.$k.'" maxlength="50" value="'.$val['send_outtime'].'" />';
				echo '	</td>';
				echo '	<td >';
				echo '		<input type="text" class="textBox send_man" id="send_man_'.$k.'" maxlength="50" value="'.$val['send_man'].'" />';
				echo '	</td>';
				foreach($send_fields as $kk=>$vv){
					echo '<td >';
					switch($vv['field_type']){
						case "char":
							echo '<input type="text" class="textBox '.$vv['id'].'" id="'.$vv['id'].'_'.$k.'" field_name="'.$vv['field_name'].'"  field_type="'.$vv['field_type'].'"  field_length="'.$vv['field_length'].'"  field_decimal_len="'.$vv['field_decimal_len'].'" field_val="'.$val[$vv['id']].'" maxlength="'.$vv['field_length'].'" value="'.$val[$vv['id']].'" />';
							break;
						case "date":
							echo '<input type="text" class="textBox '.$vv['id'].'" id="'.$vv['id'].'_'.$k.'" field_name="'.$vv['field_name'].'"  field_type="'.$vv['field_type'].'"  field_length="'.$vv['field_length'].'"  field_decimal_len="'.$vv['field_decimal_len'].'" field_val="'.$val[$vv['id']].'" maxlength="19" value="'.$val[$vv['id']].'" />';
							break;
						case "int":
							echo '<input type="text" class="textBox '.$vv['id'].'" id="'.$vv['id'].'_'.$k.'" field_name="'.$vv['field_name'].'"  field_type="'.$vv['field_type'].'"  field_length="'.$vv['field_length'].'"  field_decimal_len="'.$vv['field_decimal_len'].'" field_val="'.$val[$vv['id']].'" maxlength="'.$vv['field_length'].'" value="'.$val[$vv['id']].'" />';
							break;
						case "float":
							echo '<input type="text" class="textBox '.$vv['id'].'" id="'.$vv['id'].'_'.$k.'" field_name="'.$vv['field_name'].'"  field_type="'.$vv['field_type'].'"  field_length="'.$vv['field_length'].'"  field_decimal_len="'.$vv['field_decimal_len'].'" field_val="'.$val[$vv['id']].'" maxlength="'.($vv['field_length']+$vv['field_decimal_len']+1).'" value="'.$val[$vv['id']].'" />';
							break;
						case "per":
							echo '<input type="text" style="width:60px;text-align:right" class="textBox '.$vv['id'].'" id="'.$vv['id'].'_'.$k.'" field_name="'.$vv['field_name'].'"  field_type="'.$vv['field_type'].'"  field_length="'.$vv['field_length'].'"  field_decimal_len="'.$vv['field_decimal_len'].'" field_val="'.$val[$vv['id']].'" maxlength="3" value="'.$val[$vv['id']].'" /> %';
							break;
						case "slt":
							echo '
							<select class="textBox '.$vv['id'].'" id="'.$vv['id'].'_'.$k.'" field_name="'.$vv['field_name'].'"  field_type="'.$vv['field_type'].'"  field_length="'.$vv['field_length'].'"  field_decimal_len="'.$vv['field_decimal_len'].'" field_val="'.$val[$vv['id']].'" >';
							foreach($vv['option'] as $options){
								if($options == $val[$vv['id']]){
									echo '<option value="'.$options.'" selected="selected">'.$options.'</option>';
								}else{
									echo '<option value="'.$options.'">'.$options.'</option>';
								}	
							}
							echo '
							</select>
							';
							break;
						case "mslt":
							echo '
							<select multiple="multiple" class="textBox '.$vv['id'].'" id="'.$vv['id'].'_'.$k.'" field_name="'.$vv['field_name'].'"  field_type="'.$vv['field_type'].'"  field_length="'.$vv['field_length'].'"  field_decimal_len="'.$vv['field_decimal_len'].'" field_val="'.$val[$vv['id']].'" >';
							foreach($vv['option'] as $options){
								if($options == $val[$vv['id']]){
									echo '<option value="'.$options.'" selected="selected">'.$options.'</option>';
								}else{
									echo '<option value="'.$options.'">'.$options.'</option>';
								}	
							}
							echo '
							</select>
							';
							break;
					}
					echo '</td>';
				}
				echo '</tr>';
			}
			$rs_xiaoqu = array_values(array_unique($rs_xiaoqu));
			
			?>
		</table>
	</div>
</div>
<div  class="button_div">
	<button onclick="fnSave()">保 存</button>
	或 
	<button onclick="fnReturn()">返 回</button>
</div>
<script type="text/javascript">
var rs_source = {json_encode($rs_source)};
var rs_source_user = {json_encode($rs_source_user)};
var rs_xiaoqu = {json_encode($rs_xiaoqu)};
var pro_fields = {json_encode($pro_fields)};
var make_fields = {json_encode($make_fields)};
var send_fields = {json_encode($send_fields)};
var url_save = '/admin_source_send/modify_save';
var url_return = '/admin_source_send/index';
$().ready(function(){
	showLoading();
	o2js.build('dates');
	o2js.build('ymprompt');
});
/* 自定义字段的批量输入 */
function fnAllInputField(field_id,field_name,field_type,field_length,field_decimal_len,option){
	ymPrompt.close();
	var $messge = '';
	if(field_type == 'slt'){
		$message = '<div style="margin:10px;">请输入批量值：<select id="allInput" class="textBox" field_id="'+field_id+'"  field_name="'+field_name+'"  field_type="'+field_type+'"  field_length="'+field_length+'"  field_decimal_len="'+field_decimal_len+'"  option="'+option+'" >';
		option = option.split(',');
		for(var i=0; i<option.length; i++){
			$message += '<option value="'+option[i]+'">'+option[i]+'</option>';
		}
		$message += '</select></div>';
	}else{
		$message = '<div style="margin:10px;">请输入批量值：<input type="text" class="textBox" field_id="'+field_id+'"  field_name="'+field_name+'"  field_type="'+field_type+'"  field_length="'+field_length+'"  field_decimal_len="'+field_decimal_len+'"  option="'+option+'" maxlength="50" id="allInput" onfocus="this.select()" /></div>';
	}
	ymPrompt.confirmInfo({
		icoCls:'',msgCls:'confirm',message:$message,title:'批量输入'+field_name,height:150,handler:fnAllInputFieldEnd,autoClose:false});	
	$('#allInput').focus();
}
function fnAllInputFieldEnd(tp){
	if(tp!='ok') return ymPrompt.close();
	var field_id = $('#allInput').attr('field_id');
	var val = $('#allInput').val();
	if(val == ''){
		alert('请输入批量值！');
		$('#allInput').focus();
	}else{
		$('.tr_source .'+field_id).val(val);
		ymPrompt.close();
	}
}
/* 批量输入 */
function fnAllInput(who,n){
	ymPrompt.close();
	ymPrompt.confirmInfo({
		icoCls:'',msgCls:'confirm',message:'<div style="margin:10px;">请输入批量值：<input type="text" class="textBox" who="'+who+'" maxlength="50" id="allInput" onfocus="this.select()" /></div>',title:'批量输入'+n,height:150,handler:fnAllInputEnd,autoClose:false});	
	$('#allInput').focus();
}
function fnAllInputEnd(tp){
	if(tp!='ok') return ymPrompt.close();
	var who = $('#allInput').attr('who');
	var val = $('#allInput').val();
	if(val == ''){
		alert('请输入批量值！');
		$('#allInput').focus();
	}else{
		$('.tr_source .'+who).val(val);
		ymPrompt.close();
	}	
}
/* 特殊的批量输入 */
function fnAllInputChu(){
	var optionstr = '';
	for(var i=0; i<rs_xiaoqu.length; i++){
		optionstr += '<option value="'+rs_xiaoqu[i]+'">'+rs_xiaoqu[i]+'</option>';
	}
	ymPrompt.close();
	ymPrompt.confirmInfo({
		icoCls:'',msgCls:'confirm',message:'<div style="margin:10px;">请选择小区：<select id="allInputSlt" class="textBox">'+optionstr+'</select></div><div style="margin:10px;">输入批量值：<input type="text" class="textBox"   maxlength="19" id="allInput" onfocus="this.select()" /></div>',title:'批量输入出库时间',height:180,handler:fnAllInputChuEnd,autoClose:false});	
	$('#allInput').focus();
}
function fnAllInputChuEnd(tp){
	if(tp!='ok') return ymPrompt.close();
	var who = $('#allInputSlt').val();
	var val = $('#allInput').val();
	if(who == ''){
		alert('请选择小区！');
		$('#allInputSlt').focus();
	}
	if(val == ''){
		alert('请输入批量值！');
		$('#allInput').focus();
	}else{
		for(var i=0; i<rs_source_user.length; i++){
			if(rs_source_user[i].zone == who){
				$('#send_outtime_'+i).val(val);
			}	
		}
		ymPrompt.close();
	}	
}
/* 自动输入 */
function fnAllAuto(who){
	for(var i=0; i<rs_source_user.length; i++){
		var obj = rs_source_user[i];
		var myid = obj.id;
		switch(who){
			case "send_outtime":
				$('#send_outtime_'+i).val(rs_source.senddate+' '+'08:00:00');
				break;
			case "send_man":
				$('#send_man_'+i).val( obj.dft_send_man );
				break;
		}
	}
}
function fnReturn(){
	jumpTo(url_return);
}

/* 保存 */
function fnSave(){
	var source_user_obj = [];
	var field_obj = [];
	for(var i=0; i<rs_source_user.length; i++){
		source_user_obj.push({
			id:$.trim($('#tr_'+i).attr('myid')),
			send_outtime:$.trim($('#send_outtime_'+i).val()),
			send_man:$.trim($('#send_man_'+i).val())
		});
		for(var k=0; k<send_fields.length; k++){
			var tmpobj = {
				source_user_id:$('#tr_'+i).attr('myid')
			};		
			tmpobj['field_id'] = send_fields[k].id;
			tmpobj['field_val'] = $.trim($('#'+send_fields[k].id+'_'+i).val());
			field_obj.push(tmpobj);
		}
	}	
  $('button').attr('disabled','disabled');
	$.ajax({
		async:true,
		type:'post',
		url:url_save,
		data:{
			source_id:rs_source.id,
			source_user_obj:JSON.encode(source_user_obj), 
			field_obj:JSON.encode(field_obj) 
		},
		success:function(e){
  		$('button').attr('disabled','');
			var ok = errorHandler(e);
			if(ok){　 
				jumpTo(url_return);
			}
		}
	});
}
</script>
<!--#include file="../nav/admin_bottom.php" -->